package studyguide.ch16;

import java.io.IOException;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;
import javax.microedition.rms.RecordStoreNotOpenException;

public class RMSClientMidlet extends MIDlet {

	private final Command exitCommand;
	private final Display display;
	
	private final Form mainForm;
	private final TextBox errorBox;
	private final TextField records;
	
	private RecordStore recordStore;
	
	private class SampleCommandListener implements CommandListener {
		public void commandAction(Command command, Displayable displayable) {
			if (command == exitCommand) {
				destroyApp(true);
				notifyDestroyed();
			} else {
				throw new RuntimeException("UNKNOWN COMMAND" + command);
			}
		}
	}
	
	public RMSClientMidlet() throws IOException {
		mainForm = new Form("Client RMS Sample");
		errorBox = new TextBox("Error", "Database does not exist. Install server and try again", 500, TextField.UNEDITABLE);
		exitCommand = new Command("Exit", Command.EXIT, 1);
		
		mainForm.addCommand(exitCommand);
		errorBox.addCommand(exitCommand);
		
		SampleCommandListener listener = new SampleCommandListener();
		mainForm.setCommandListener(listener);
		errorBox.setCommandListener(listener);
		display = Display.getDisplay(this);
		
		records = new TextField("Records:", "", 500, TextField.UNEDITABLE);
		
		mainForm.append(records);
	}
	
	protected void destroyApp(boolean unconditional) {
		try {
			closeDatabase();
		} catch (Exception t) {
			t.printStackTrace();
			// Exit anyway
		}
	}

	protected void pauseApp() {
		try {
			closeDatabase();
		} catch (Exception t) {
			t.printStackTrace();
			// Pause anyway
		}
	}

	protected void startApp() throws MIDletStateChangeException {
		try {
			openDatabase();
			
			// Update presentation with database status
			records.delete(0, records.size());
			RecordEnumeration enRecs = recordStore.enumerateRecords(null, null, false);
			while (enRecs.hasNextElement()) {
				byte[] serializedData = enRecs.nextRecord();
				String record = new String(serializedData);
				records.insert(record + "\n", records.size());
			}
			enRecs.destroy();
			display.setCurrent(mainForm);		
		} catch (Exception e) {
			errorBox.insert(" Exception:" + e, errorBox.size());
			display.setCurrent(errorBox);
		}
	}

	private void openDatabase() throws RecordStoreNotFoundException {
		try {
			recordStore = RecordStore.openRecordStore("MyOtherRecordStore", "eaf-study-guides", "RMS-Server");
		} catch (RecordStoreNotFoundException e) {
			throw new RuntimeException(e.getMessage());
		} catch (RecordStoreFullException e) {
			throw new RuntimeException(e.getMessage());
		} catch (RecordStoreException e) {
			throw new RuntimeException(e.getMessage());
		}
	}

	private void closeDatabase() {
		try {
			recordStore.closeRecordStore();
		} catch (RecordStoreNotOpenException e) {
			// No problem here
		} catch (RecordStoreException e) {
			throw new RuntimeException(e.getMessage());
		}
	}

}
